#!/bin/sh

. /usr/share/acpi-support/key-constants

# The sequence here *is* important.
unload_modules() {
   modprobe -r -q ath_pci
   modprobe -r -q ath_rate_sample
   modprobe -r -q ath_hal
   modprobe -r -q wlan_ccmp
   modprobe -r -q wlan_tkip
   modprobe -r -q wlan_wep
   modprobe -r -q wlan_acl
   modprobe -r -q wlan_scan_sta
   modprobe -r -q wlan
}

# At least this one's straightforward.
load_modules() {
   modprobe ath_pci
}

wifi_on() {
   # Force PCI Express Hotplug to reinit
   modprobe -r -q pciehp
   sleep 1
   # pciehp_force may be unnecessary; Xandros did it.
   modprobe pciehp pciehp_force=1
   sleep 1
   # Switch on the hardware
   echo 1 >/proc/acpi/asus/wlan
   sleep 1
   load_modules
   sleep 1
   /etc/init.d/networking restart
}

wifi_off() {
   unload_modules
   echo 0 >/proc/acpi/asus/wlan
}

case $1 in
   on)
      acpi_fakekey $KEY_PROG1
      wifi_on
      ;;
   off)
      acpi_fakekey $KEY_PROG2
      wifi_off
      ;;
   toggle)
      STAT=`cat /proc/acpi/asus/wlan`
      if [ "$STAT" = "1" ];
         then wifi_off;
         else wifi_on;
      fi
      ;;
esac